﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;
using PSE.Framework.ErrorLogging;

namespace PSE.Updater.Client.Providers.Updates
{
    public class ScheduleExecutation
    {
        private const string CONST_APPLICATION_KEY = "PSEUpdaterApplication";
        private const string CONST_KEY_DATETIME = "DateTime";
        private const string CONST_KEY_UPDATE_ON_WIN_START = "UpdateOnWinStart";

        RegistryKey _applicationKey = null;

        #region [ Singleton ]
        private static object _lock = new object();
        private static ScheduleExecutation _instance;
        public static ScheduleExecutation Instance
        {
            get
            {
                lock (_lock)
                {
                    if (_instance == null)
                        _instance = new ScheduleExecutation();
                }
                return _instance;
            }
        }
        private ScheduleExecutation() 
        {
            RegistryKey key = Registry.CurrentUser.OpenSubKey("Software", true);
            _applicationKey = key.CreateSubKey(CONST_APPLICATION_KEY);
        }

        #endregion [ Singleton ]

        public void Add(DateTime date)
        {
            _applicationKey.SetValue(CONST_KEY_DATETIME, date.ToString());
        }

        public DateTime? GetDateTime()
        {
            object @value = _applicationKey.GetValue(CONST_KEY_DATETIME, null);
            if (@value == null)
                return null;

            DateTime? dateTime = null;

            try
            {
                dateTime = DateTime.Parse(@value.ToString());
            }
            catch (Exception ex)
            {
                ErrorLogger.WriteLog(LogType.Error, ex);
            }

            return dateTime;
        }

        public void Remove()
        {
            try
            {
                Registry.CurrentUser.DeleteSubKey(@"Software\" + CONST_APPLICATION_KEY);
            }
            catch (Exception ex)
            {
                ErrorLogger.WriteLog(LogType.Error, ex);
            }
        }

        public bool GetUpdateOnWinStart()
        {
            object @value = _applicationKey.GetValue(CONST_KEY_UPDATE_ON_WIN_START, null);
            if (@value == null)
                return false;

            bool updateOnWinStart = false;

            try
            {
                updateOnWinStart = bool.Parse(@value.ToString());
            }
            catch (Exception ex)
            {
                ErrorLogger.WriteLog(LogType.Error, ex);
            }

            return updateOnWinStart;
        }

        public void SetUpdateOnWinStart()
        {
            try
            {
                _applicationKey.SetValue(CONST_KEY_UPDATE_ON_WIN_START, true.ToString());
            }
            catch (Exception ex)
            {
                ErrorLogger.WriteLog(LogType.Error, ex);
            }
        }

        public void RemoveUpdateOnWinStart()
        {
            try
            {
                _applicationKey.SetValue(CONST_KEY_UPDATE_ON_WIN_START, false.ToString());
            }
            catch (Exception ex)
            {
                ErrorLogger.WriteLog(LogType.Error, ex);
            }
        }

        public bool IsScheduled()
        {
            var dateTime = GetDateTime();
            var updateOnWinStart = GetUpdateOnWinStart();

            if (dateTime.HasValue || updateOnWinStart)
                return true;

            return false;
        }
    }
}